<?php

App::uses('Crud', 'Model');
App::uses('WorkstationConfig', 'Model');
App::uses('Workstation', 'Model');
App::uses('Certification', 'Model');

class CertificationDetail extends Crud
{
    /**
     * Al Agregar/eliminar/actualizar las constantes de los modelos implica ir
     * y actualizar los comentarios en los campos de la tabla asociada a este modelo.
     */
    const STATUS_ENABLED = 1;
    const STATUS_DISABLED = 0;
    public $useTable = 'certifications_details';

    public function getByCertification($certificationId, $userId)
    {
        $filters = array(
            'fields' => array(
                "\"$userId\" as user_id",
                'wc.id as workstation_config_id',
                'wc.name as workstation_config_name',
                'wc.minimum_qualification as workstation_config_qualification',
                'CertificationDetail.id as certification_detail_id',
                'CertificationDetail.qualification as certification_detail_qualification',
                'CertificationDetail.certification_date as certification_detail_date',
                'TIMESTAMPDIFF(MINUTE, CertificationDetail.creation_date, now()) as minutes',
                'CertificationDetail.creation_date as certification_detail_creation_date',
            ),
            'joins' => array(
                array(
                    'table' => 'workstations_configs',
                    'alias' => 'wc',
                    'type' => 'INNER',
                    'conditions' => array(
                        'CertificationDetail.workstation_config_id = wc.id'
                    ),
                ),
            ),
            'conditions' => array(
                'CertificationDetail.certification_id' => $certificationId,
                'CertificationDetail.status' => self::STATUS_ENABLED,
            ),
            'group' => array(
                'CertificationDetail.id',
                'CertificationDetail.qualification',
                'CertificationDetail.certification_date',
                'CertificationDetail.creation_date',
                'wc.id',
                'wc.name',
                'wc.minimum_qualification',
            ),
            'order' => array(
                'wc.orden' => 'ASC',
            ),
        );
        $records = $this->find('all', $filters);
        return $this->flatArray($records);
    }

    public function disabledDetailsByCertification($certificationId) {
        $fields = array(
            'status' => CertificationDetail::STATUS_DISABLED,
        );
        $conditions = array(
            'certification_id' => $certificationId,
        );
        $this->updateAll($fields, $conditions);
    }

    public function isCertificationSuccess($certificationId, $workstationId) {
        $filters = array(
            'fields' => array(
                'wc.id'
            ),
            'joins' => array(
                array(
                    'table' => 'workstations_configs',
                    'alias' => 'wc',
                    'type' => 'INNER',
                    'conditions' => array(
                        'CertificationDetail.workstation_config_id = wc.id'
                    ),
                ),
            ),
            'conditions' => array(
                'CertificationDetail.status' => self::STATUS_ENABLED,
                'CertificationDetail.certification_id' => $certificationId,
                'OR' => array(
                    'CertificationDetail.qualification IS NULL',
                    'AND' => array(
                        'CertificationDetail.qualification IS NOT NULL',
                        'CertificationDetail.qualification < wc.minimum_qualification',
                    )
                )
            ),
        );
        $records = $this->find('all', $filters);
        return count($records) == 0;
    }

    public function updateQualificationAndDate($id, $qualification, $certificationDate)
    {
        $fields = array(
            'qualification' => $qualification,
            'certification_date' => $certificationDate,
        );
        $this->update($id, $fields);
    }

    public function clearLinks() {
        $fields = array(
            'source' => null,
        );
        $this->updateAll($fields);
    }

    public function getOkByUserAndLineAndType($userId, $lineId, $type) {
        $filters = array(
            'fields' => array(
                'w.description as workstation',
            ),
            'joins'=> array(
                array(
                    'table'=>'certifications',
                    'alias'=>'c',
                    'type' => 'INNER',
                    'conditions'=>array(
                        'c.id = CertificationDetail.certification_id',
                        'c.status'=> Certification::STATUS_ENABLED
                    ),
                ),
                array(
                    'table'=>'workstations_configs',
                    'alias'=>'wc',
                    'type' => 'INNER',
                    'conditions'=>array(
                        'wc.id = CertificationDetail.workstation_config_id',
                        'wc.status'=> WorkstationConfig::STATUS_ENABLED,
                    ),
                ),
                array(
                    'table'=>'workstations',
                    'alias'=>'w',
                    'type' => 'INNER',
                    'conditions'=>array(
                        'wc.workstation_id = w.id',
                        'w.line_id' => $lineId,
                        'w.status'=> Workstation::STATUS_ENABLED,
                    ),
                ),
            ),
            'conditions' => array(
                'CertificationDetail.status' => self::STATUS_ENABLED,
                'CertificationDetail.qualification >= wc.minimum_qualification',
                'c.user_id'=> $userId,
                'wc.name' => $type,
            ),
        );
        return $this->flatArray($this->find('all', $filters));
    }
    
    public function getNokByUserAndLineAndType($userId, $lineId, $type) {
        $filters = array(
            'fields' => array(
                'w.description as workstation',
            ),
            'joins'=> array(
                array(
                    'table'=>'certifications',
                    'alias'=>'c',
                    'type' => 'INNER',
                    'conditions'=>array(
                        'c.id = CertificationDetail.certification_id',
                        'c.status'=> Certification::STATUS_ENABLED,
                    ),
                ),
                array(
                    'table'=>'workstations_configs',
                    'alias'=>'wc',
                    'type' => 'INNER',
                    'conditions'=>array(
                        'wc.id = CertificationDetail.workstation_config_id',
                        'wc.status'=> WorkstationConfig::STATUS_ENABLED,
                    ),
                ),
                array(
                    'table'=>'workstations',
                    'alias'=>'w',
                    'type' => 'INNER',
                    'conditions'=>array(
                        'wc.workstation_id = w.id',
                        'w.line_id' => $lineId,
                        'w.status'=> Workstation::STATUS_ENABLED
                    ),
                ),
            ),
            'conditions' => array(
                'CertificationDetail.status' => self::STATUS_ENABLED,
                'CertificationDetail.qualification < wc.minimum_qualification',
                'c.user_id'=> $userId,
                'wc.name' => $type,
            ),
        );
        return $this->flatArray($this->find('all', $filters));
    }

    /**
     * Buscamos si existe una parte de una certificacion
     * @param $certificationId string UUID
     * @param $workstationConfigId string UUID
     * @return array
     */
    public function getByCertificationAndWorkstationConfig($certificationId, $workstationConfigId)
    {
        $filters = array(
            'conditions' => array(
                'certification_id' => $certificationId,
                'workstation_config_id' => $workstationConfigId,
                'status' => self::STATUS_ENABLED,
            ),
        );
        return $this->find('first', $filters);
    }

}
